package com.example.demo.generator.codegen.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 描述 CREATE TABLE 语句中的索引/约束信息。
 */
public class IndexInfo {

    public enum IndexType {
        PRIMARY,
        UNIQUE,
        INDEX,
        FULLTEXT,
        SPATIAL,
        UNKNOWN
    }

    private String name;
    private IndexType type = IndexType.UNKNOWN;
    private List<String> columnNames = new ArrayList<>();

    public boolean isUnique() {
        return type == IndexType.PRIMARY || type == IndexType.UNIQUE;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public IndexType getType() {
        return type;
    }

    public void setType(IndexType type) {
        this.type = type != null ? type : IndexType.UNKNOWN;
    }

    public List<String> getColumnNames() {
        return columnNames;
    }

    public void setColumnNames(List<String> columnNames) {
        this.columnNames = columnNames != null ? columnNames : new ArrayList<>();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof IndexInfo)) {
            return false;
        }
        IndexInfo indexInfo = (IndexInfo) o;
        return Objects.equals(name, indexInfo.name)
                && type == indexInfo.type
                && Objects.equals(columnNames, indexInfo.columnNames);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, type, columnNames);
    }

    @Override
    public String toString() {
        return "IndexInfo{" +
                "name='" + name + '\'' +
                ", type=" + type +
                ", columnNames=" + columnNames +
                '}';
    }
}
